<?php
	defined('RUN') or die('No access');

	class CategoryController
	{
		public function __construct()
		{
			if (!Auth::check('acp'))
			{
				Url::redirect('admin/auth/login');
			}
		}
		
		public function index()
		{
			$data = Model::get('admin')->getLayoutData();

			$data['categories'] = Model::get('category')->getCategoriesAll();

			$data['content_file'] = 'category/view.tpl';
			View::load('layout.tpl', $data, 'admin');
		}

		public function add()
		{
			$data = Model::get('admin')->getLayoutData();

			if (Input::post('action') == 'add' && Security::checkToken('category_form', Input::post('token')))
			{
				if (Validator::checkRequired(Input::post(), array('name')))
				{
					$category = array(
						'name' => strip_tags(Input::post('name')),
						'description' => strip_tags(Input::post('description')),
						'keywords' => strip_tags(Input::post('keywords')),
                        'parent_id' => Input::post('parent_id'),
                        'status'    => Input::post('status'),
                        'sort'    => Input::post('txtSort')						
					);

					if (Model::get('category')->add($category))
					{
						Session::setFlash('notification', array('type' => 'success', 'msg' => 'Added a new post !'));
						Url::redirect('admin/category');
					}
				}

				$data['failed'] = TRUE;
			}
            
			$data['categories'] = Model::get('category')->getCategoriesAll();
			$data['parent_id'] = 0;
                        
			$data['action'] = 'add';
			$data['content_file'] = 'category/form.tpl';
			View::load('layout.tpl', $data, 'admin');
		}
        
        public function update($category_id)
		{
			$data = Model::get('admin')->getLayoutData();
            $data['category'] = Model::get('category')->getById($category_id);

			if (Input::post('action') == 'update' && Security::checkToken('category_form', Input::post('token')))
			{
				if (Validator::checkRequired(Input::post(), array('name')))
				{
					$category = array(
						'name' => strip_tags(Input::post('name')),
						'description' => strip_tags(Input::post('description')),
						'keywords' => strip_tags(Input::post('keywords')),
                        'parent_id' => Input::post('parent_id'),
                        'status'    => Input::post('status'),
                        'sort'    => Input::post('txtSort')							
					);

					if (Model::get('category')->update($category, $category_id))
					{
						Session::setFlash('notification', array('type' => 'success', 'msg' => 'Updated a post !'));
						Url::redirect('admin/category');
					}
				}

				$data['failed'] = TRUE;
			}
			//select
			$categories = Model::get('category')->getCategoriesAll();
    		if (!empty($category_id)) {
    			foreach ($categories as $key => $category) {
    				if ($category['id'] == $category_id) {
    					unset($categories[$key]);
    				}
    			}
    		}
            $data['categories'] = $categories;
            
			$data['action'] = 'update';
			$data['content_file'] = 'category/form.tpl';
			View::load('layout.tpl', $data, 'admin');
		}                

		public function delete($category_id, $confirm = 'no')
		{
			$data = Model::get('admin')->getLayoutData();

			if ($confirm == 'yes')
			{
				$subchange = Model::get('category')->getCategoriesAdm($category_id);      
                if($subchange){
                    foreach($subchange as $sub){
                        $sub_data['parent_id'] = 0;
                        Model::get('category')->update($sub_data,$sub['id']);
                    }
                }  
                
                if (Model::get('category')->delete($category_id))
				{
					Session::setFlash('notification', array('type' => 'success', 'msg' => 'Deleted a category !'));
					Url::redirect('admin/category');
				}
			}

			$data['category'] = Model::get('category')->getById($category_id);
			$data['content_file'] = 'category/delete.tpl';
			View::load('layout.tpl', $data, 'admin');
		}
	}

